﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace SCFHS.Views.Reports
{
    public partial class OrderFollowupByNumber : SysBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                if (!string.IsNullOrEmpty(txtOrderNumber.Text))
                {
                    BindReport(txtOrderNumber.Text);
                    return;
                }
            }
            if (Request.QueryString["orderId"] != null && !IsPostBack)
            {
                string orderID = Request.QueryString["orderId"].ToString();
                if (!string.IsNullOrEmpty(orderID))
                {
                    txtOrderNumber.Text = orderID;
                    BindReport(orderID);
                }
            }
        }

        protected void btnShowReport_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtOrderNumber.Text))
                Response.Redirect("OrderFollowupByNumber.aspx?orderId=" + txtOrderNumber.Text);
        }

        //DataTable HandleHeaderColumn(DataTable dt, string headerText)
        //{
        //    DataColumn dc = new DataColumn("DetailHeader");
        //    dc.DefaultValue = headerText;
        //    dt.Columns.Add(dc);
        //    dc = new DataColumn("Indexer");
        //    dt.Columns.Add(dc);
        //    for (int i = 0; i < dt.Rows.Count; i++)
        //    {
        //        dt.Rows[i]["Indexer"] = (i + 1).ToString();
        //    }
        //    return dt;
        //}

        void BindReport(string orderNumber)
        {
            rptViewer.ID = "Report Number " + orderNumber + "";
            DataTable orderFollowupDt = new DataTable();

            order = controller.SelectMainOrderInfo(orderNumber);
            orderFollowupDt = controller.ListInstructionsForOrder(order.orderID);
            if (orderFollowupDt != null)
            {
                DataTable tempFollowups = controller.ListFollowupsForOrder(order.orderID);

                foreach (DataColumn dc in tempFollowups.Columns)
                {
                    if (!orderFollowupDt.Columns.Contains(dc.ColumnName))
                        orderFollowupDt.Columns.Add(dc.ColumnName, dc.DataType);
                }
                int indx = 0;
                foreach (DataRow dr in tempFollowups.Rows)
                {
                    foreach (DataColumn dc in tempFollowups.Columns)
                    {
                        if (orderFollowupDt.Rows[indx][dc.ColumnName].ToString() == "")
                            orderFollowupDt.Rows[indx][dc.ColumnName] = dr[dc].ToString();
                    }
                    indx++;
                }
            }
            ParameterFields rptParams = new ParameterFields();
            rptParams.Add(HandleRptParams("PersonName", order.personName));
            rptParams.Add(HandleRptParams("OrderNumber", order.orderNumber));
            rptParams.Add(HandleRptParams("ClassificationNumber", order.orderClassificationNumber));
            BindRpt(orderFollowupDt, rptParams);
        }

        ReportDocument report;

        void BindRpt(DataTable orderDtSrc, ParameterFields rptParams)
        {
            rptViewer.ReportSource = new ReportDocument();

            if (orderDtSrc != null)
            {
                rptViewer.Visible = true;
                report = new ReportDocument();
                report.Load(Server.MapPath("Design/OrderFollowup.rpt"));
                report.SetDataSource(orderDtSrc);
                report.SummaryInfo.ReportTitle = "متابعة لطلب رقم " + order.orderNumber;
                rptViewer.ReportSource = report;
                rptViewer.ParameterFieldInfo = rptParams;
                //hdfOrderNumber.Value = orderDtSrc.Rows[0]["ID"].ToString();
                //btnToInstructions.Visible = true;
                rptViewer.DataBind();

            }
            else
            {
                lblMessage.Text = "عفوا لا يوجد بيانات لهذا الطلب";
                lblMessage.CssClass = "error";
            }
        }

        //private DataTable ClearDetailsStyle(DataTable orderDt)
        //{
        //    for (int i = 1; i < orderDt.Rows.Count; i++)
        //    {
        //        orderDt.Rows[i - 1]["DetailHeader"] = orderDt.Rows[i]["DetailHeader"];
        //        orderDt.Rows[i - 1]["Indexer"] = orderDt.Rows[i]["Indexer"];
        //        orderDt.Rows[i - 1]["Point"] = orderDt.Rows[i]["Point"];
        //        orderDt.Rows[i - 1]["Fk_OrderID"] = orderDt.Rows[i]["Fk_OrderID"];
        //    }
        //    orderDt.Rows.RemoveAt(orderDt.Rows.Count - 1);
        //    return orderDt;
        //}

        private ParameterField HandleRptParams(string name, string value)
        {
            ParameterDiscreteValue rptDiscreteVal = new ParameterDiscreteValue();
            ParameterField rptParam = new ParameterField();

            rptDiscreteVal.Value = value;
            rptParam.Name = name;
            rptParam.CurrentValues.Add(rptDiscreteVal);

            return rptParam;
        }


        protected void Page_Unload(object sender, EventArgs e)
        {
            ReportDocument report = rptViewer.ReportSource as ReportDocument;
            rptViewer.ReportSource = null;
            if (report == null)
                return;
            report.Database.Dispose();
            report.Close();
            report.Dispose();
            GC.Collect();

        }
    }
}